#include <stdio.h>
#include <omp.h>

int main() {
    int x = 1;
    
    printf("Initial x = %d\n", x);
    printf("Running OpenMP tasks with inverted dependencies...\n");

    #pragma omp parallel       // 并行区域
    #pragma omp single         // 单线程生成任务
    {
        // 任务1：读取x（依赖输入x）
        #pragma omp task shared(x) depend(in: x)
        {
            printf("Task 1 (Thread %d): Read x = %d\n", omp_get_thread_num(), x);
        }

        // 任务2：写入x=2（依赖输出x）
        #pragma omp task shared(x) depend(out: x)
        {
            x = 2;
            printf("Task 2 (Thread %d): Set x = %d\n", omp_get_thread_num(), x);
        }
    }

    printf("Final x = %d\n", x);
    return 0;
}